<template>
  <section>
    <!--工具条 - 高级查询框-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSale">上架</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="offSale">下架</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" height="650"
              @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55" fixed>
      </el-table-column>
      <el-table-column type="index" width="60" fixed>
      </el-table-column>
      <el-table-column prop="title" label="名称" fixed width="120" sortable>
      </el-table-column>
      <el-table-column prop="cover" label="封面图" width="120" sortable>
        <template slot-scope="scope">
          <img :src="scope.row.cover" width="30px" height="30px">
        </template>
      </el-table-column>
      <el-table-column prop="saleprice" label="原价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="costprice" label="折扣价" width="100" sortable>
      </el-table-column>
      <el-table-column prop="isnew" label="是否新车" width="120" sortable>
        <template slot-scope="scope">
          <span v-if="scope.isnew == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="reigstertime" label="上牌时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="mileage" label="里程数" width="100" sortable>
      </el-table-column>
      <el-table-column prop="shopname" label="店铺名称" width="100" sortable>
      </el-table-column>
      <el-table-column prop="onsaletime" label="上架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="offsaletime" label="下架时间" width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="40" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1" style="color: blue">上架</span>
          <span v-else>下架</span>
        </template>
      </el-table-column>
      <el-table-column prop="costeffective" label="超值" min-width="40" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.costeffective == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="rushsale" label="急售" min-width="40" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.rushsale == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="quasinewcar" label="准新车" min-width="40" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.quasinewcar == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="transitivecountry" label="可提全国" min-width="40" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.transitivecountry == 1">是</span>
          <span v-else>否</span>
        </template>
      </el-table-column>
      <el-table-column prop="type.name" label="类型" width="100" sortable>
      </el-table-column>
      <el-table-column prop="seller.username" label="营销人员" width="100" sortable>
      </el-table-column>
      <el-table-column prop="auditstate" label="审核状态" width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.auditstate == 1">已审核</span>
          <span v-else>未审核</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="350" fixed="right">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="danger" size="small" @click="conservation(scope.row)">资源维护</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">检测报告</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 抽屉-->
    <el-drawer
        title="车辆详情信息"
        :visible.sync="table"
        direction="rtl"
        size="50%">

      <el-button size="small" type="primary" @click="carDetailInsert" style="margin-left:15px">新增</el-button>
      <el-table :data="gridData">
        <el-table-column property="carDetail.cartitle" label="车辆信息" width="200"></el-table-column>

        <!--        property="carDetail.info"-->
        <el-table-column label="车辆图片" width="300">
          <template slot-scope="scope">
            <p v-html="scope.row.carDetail.info" class="detailImg"></p>
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="150">
          <template slot-scope="scope">
            <el-button size="small" @click="carDetailEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="carDetailDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>

    <!-- 车辆资源新增-->
    <el-dialog title="车辆资源新增" :visible.sync="carDetailInsertFormVisible" :close-on-click-modal="false">
      <el-form label-width="120px" ref="carResourceForm">

        <el-form-item label="资源类型">
          <el-select v-model="resourceListOption">
            <el-option
                v-for="list in resourceList"
                :key="list"
                :label="list"
                :value="list">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="资源文件">
          <el-upload
              class="upload-demo"
              action="http://localhost:9002/common/upload"
              :on-remove="coverHandleRemove"
              :on-success="coverHandleSuccess"
              :file-list="coverFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carDetailInsertFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="carResouceAdd" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--工具条 - 批量删除&分页条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="prev, pager, next"
          :current-page="query.currentPage"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :total="pageInfo.total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增界面-->
    <el-dialog title="新增/修改" :visible.sync="carFormVisible" :close-on-click-modal="false">
      <el-form :model="carForm" label-width="80px" ref="carForm">
        <el-form-item label="标题">
          <el-input v-model="carForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="封面">
          <el-upload
              class="upload-demo"
              action="http://localhost:9002/common/upload"
              :on-remove="coverHandleRemove"
              :on-success="coverHandleSuccess"
              :file-list="coverFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="原价">
          <el-input v-model="carForm.saleprice" style="width:220px;" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="售价">
          <el-input v-model="carForm.costprice" style="width:220px;" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="是否新车">
          <el-radio-group v-model="carForm.isnew">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上牌时间">
          <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="carForm.reigstertime"
              type="date"
              placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="里程数">
          <el-input v-model="carForm.mileage" style="width:220px;" type="number" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="超值">
          <el-radio-group v-model="carForm.costeffective">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="急售">
          <el-radio-group v-model="carForm.rushsale">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="准新车">
          <el-radio-group v-model="carForm.quasinewcar">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可迁全国">
          <el-radio-group v-model="carForm.transitivecountry">
            <el-radio class="radio" :label="1">是</el-radio>
            <el-radio class="radio" :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="车辆品牌">
          <el-select v-model="carForm.brand" placeholder="请选择">
            <el-option
                v-for="carType in carTypes"
                :key="carType.value"
                :label="carType.name"
                :value="carType.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆类型">
          <el-cascader v-model="carForm.type"
                       :options="carTypeTree"
                       :props="{
							   checkStrictly: true,
							   label:'name',
							   value:'id'
							}"
                       clearable></el-cascader>
        </el-form-item>
        <el-form-item label="基本信息">
          <!--<el-input type="textarea" v-model="carForm.carDetail.info"></el-input>-->
          <quill-editor v-model="carForm.carDetail.info" :options="quillOptions">
          </quill-editor>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 车辆详情新增编辑页面弹框-->
    <el-dialog title="车辆信息新增编辑" :visible.sync="carDetailFormVisible" :close-on-click-modal="false">
      <el-form :model="gridDataObj" label-width="120px" ref="carDetailForm">
        <el-form-item label="车辆信息">
          <el-input v-model="gridDataObj.title" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="车辆图片类型">
          <el-select v-model="resourceListOption" @change="changeResouce">
            <el-option
                v-for="res in resourceList"
                :key="res"
                :label="res"
                :value="res"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="封面">
          <el-upload
              class="upload-demo"
              action="http://localhost:9002/common/upload"
              :on-remove="coverHandleRemove"
              :on-success="coverHandleSuccess"
              :file-list="coverFileList"
              list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="车辆所在店铺" label-width="120px">
          <el-input v-model="gridDataObj.shopname" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="车辆原价" label-width="120px">
          <el-input v-model="gridDataObj.costprice" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="车辆售价" label-width="120px">
          <el-input v-model="gridDataObj.saleprice" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="车辆下架时间" label-width="120px">
          <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="gridDataObj.offsaletime"
              type="date"
              placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆上架时间" label-width="120px">
          <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="gridDataObj.onsaletime"
              type="date"
              placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <!--        <el-form-item label="车辆下架时间" label-width="120px">-->
        <!--          <el-input v-model="gridDataObj.offsaletime" auto-complete="off"></el-input>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="车辆上架时间" label-width="120px">-->
        <!--          <el-input v-model="gridDataObj.onsaletime" auto-complete="off"></el-input>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="carDetailFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="carDetailSave" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import {quillEditor} from "vue-quill-editor"; // 在页面中引入富文本编辑框的组件
import 'quill/dist/quill.core.css';   // 引入样式
import 'quill/dist/quill.snow.css';
import 'quill/dist/quill.bubble.css';
import quillConfig from "../../common/js/quill-config.js"

import axios from "axios";

export default {
  components: {
    quillEditor   // 如果key和value的名字一样,可以省略 quillEditor:quillEditor
  },
  data() {
    return {
      carResourceList: {
        carId: null,
        type: '',
        cover: [],
      },// 资源新增数据
      resourceListOption: "",// 资源类型
      resourceList: ["车辆图片", "车辆细节图", "车辆检测报告", "车辆视频", "车辆vr"],// 资源类型
      carDetailInsertFormVisible: false,// 车辆资源新增弹框
      gridDataObj: {
        resource: []
      },// 车辆详请编辑数据
      carDetailFormVisible: false,// 车辆详请编辑弹框
      table: false,// 抽屉弹出
      gridData: [], // 抽屉数据
      quillOptions: quillConfig,
      query: {
        currentPage: 1,  // 当前页
        pageSize: 10,  // 每页显示条数
        keyword: ""  // 关键字
      },
      pageInfo: {
        total: 0,
        rows: []
      },
      listLoading: false,
      sels: [],//列表选中列

      carFormVisible: false,//新增修改界面是否显示
      carFormLoading: false,
      carFormRules: {
        name: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      },
      employees: [],
      carTypeTree: [],
      addLoading: false,
      coverFileList: [], //name url
      carTypes: [], // 车辆品牌
      //新增修改界面数据
      carForm: {
        id: null,
        title: "",
        cover: "",
        saleprice: 0,
        costprice: 0,
        isnew: 0,
        reigstertime: "",
        mileage: null,
        costeffective: 0,
        rushsale: 0,
        quasinewcar: 0,
        transitivecountry: 0,
        type: {
          id: null,
          name: ""
        },
        brand: null,
        typeId: null,
        carDetail: {
          info: ""
        }
      }
    }
  },
  methods: {
    // 车辆上架
    onSale() {
      let ids = this.sels.map(item => item.id);
      this.$confirm('确认上架所选车辆吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        axios.post("/car/onSale", ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          if (res.status == 200) {
            this.$message.success(res.data.message);
          }
        });
      }).catch(() => {

      }).finally(() => {
        this.getCars();
      })

    },
    // 车辆下架
    offSale() {
    },
    // 车辆资源新增
    carResouceAdd() {
      let resource_type = null;
      // 1.车辆图片  2.车辆细节图  3.车辆检测报告  4.车辆视频 5.车辆vr

      // 数据处理
      if (this.resourceListOption == "车辆图片") resource_type = 1
      if (this.resourceListOption == "车辆细节图") resource_type = 2
      if (this.resourceListOption == "车辆检测报告") resource_type = 3
      if (this.resourceListOption == "车辆视频") resource_type = 4
      if (this.resourceListOption == "车辆vr") resource_type = 5


      let para = {
        carId: this.carResourceList.carId,
        resourceUrl: this.carResourceList.cover.toString(),
        resourceType: resource_type
      }
      console.log(para)
      axios.post("/carResources", para).then(res => {
        console.log(res)
        if (res.status == 200) {
          this.$message.success(res.data.message)
          // 弹框关闭
          this.carDetailInsertFormVisible = false
          // 数据清空
          this.carResourceList = {}
        }
      }).catch(() => {

      })
    },
    // 车辆详情新增
    carDetailInsert() {
      // 弹框弹出
      this.carDetailInsertFormVisible = true
      this.carResourceList.carId = this.gridData[0].id
    },
    //车辆详情删除
    carDetailDel(row) {
      console.log(row.id)

      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        axios.delete("/carDetail/" + row.id)
            .then(res => {
              if (res.status == 200) {
                this.$message({message: '删除成功', type: 'success'});
                // 弹框关闭
                this.carDetailFormVisible = false
                // 抽屉关闭
                this.table = false
                // 查询数据
                this.getCars();
              } else {
                this.$message({message: '删除失败!' + res.message, type: 'error'});
              }
            })
      }).catch(() => {
        this.$message({message: '已取消删除', type: 'info'});
      });
      // axios.delete("/carDetail/" + row.id).then(res => {
      //   if (res.status == 200) {
      //     this.$message.success(res.data.message)
      //     // 弹框关闭
      //     this.carDetailFormVisible = false
      //     // 抽屉关闭
      //     this.table = false
      //   }
      // }).catch(() => {
      // }).finally(() => {
      //   this.getCars();
      // })
    },
    // 车辆详情编辑
    carDetailEdit: function (row) {
      // 弹框
      this.carDetailFormVisible = true;
      this.getCarTypes();
      axios.get("/car/" + row.id).then(res => {
        if (res.status == 200) {
          // 回显数据
          this.gridDataObj = res.data.data
          this.gridDataObj.resource = []
          this.getResource(row.id);
          // 回显图片
          this.coverFileList.push({name: "car.jpg", url: res.data.data.cover})
        }

      }).catch(() => {
      }).finally(() => {
      })
    },
    // 资源数据改变
    changeResouce: function () {
      this.coverFileList=[]
      this.gridDataObj.resource.map(item => {
        item.map(i => {
          i.resourceUrl.split(",").map(j => {
            if (this.resourceListOption == "车辆图片") {
              if (i.resourceType == 1) {
                this.coverFileList.push({
                  name: "车辆图片.png",
                  url: j
                })
              }
            }
            if (this.resourceListOption == "车辆细节图") {
              if (i.resourceType == 2) {
                this.coverFileList.push({
                  name: "车辆细节图.png",
                  url: j
                })
              }
            }
            if (this.resourceListOption == "车辆检测报告") {
              if (i.resourceType == 3) {
                this.coverFileList.push({
                  name: "车辆检测报告.png",
                  url: j
                })
              }
            }
            if (this.resourceListOption == "车辆视频") {
              if (i.resourceType == 4) {
                this.coverFileList.push({
                  name: "车辆视频.png",
                  url: j
                })
              }
            }
            if (this.resourceListOption == "车辆vr") {
              if (i.resourceType == 5) {
                this.coverFileList.push({
                  name: "车辆vr.png",
                  url: j
                })
              }
            }
          })
        })
      })
    },
    // 获取车辆资源信息
    getResource(id) {
      axios.post("/carResources/getListResouce/" + id).then(res => {
        if (res.status == 200) {
          this.gridDataObj.resource.push(res.data.data)
        }
      }).catch(() => {

      })
    },
    // 车辆详情提交更新
    carDetailSave(row) {
      this.$refs.carDetailForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            let para = Object.assign({}, this.gridDataObj);
            axios.post("/car", para).then(res => {
              if (res.status == 200) {
                // 更新成功
                // 关闭弹框
                this.carDetailFormVisible = false
                this.$message.success("修改成功")
                // 编辑框数据清除
                this.gridDataObj = {}
                // 回显图片数据清除
                this.coverFileList = []
                // 抽屉返回
                this.table = false
              }

            }).catch(() => {
            }).finally(() => {
              this.getCars();
            })
          });
        }
      });
    },
    // 资源维护
    conservation(row) {
      // 弹出抽屉
      this.table = true;
      // 根据id查看车辆详情
      axios.get("/car/" + row.id).then(res => {
        if (res.status == 200) {
          this.gridData = [res.data.data]
          console.log(this.gridData)
        }

      }).catch(() => {

      })
    },
    // 成功回调
    coverHandleSuccess(response, file, fileList) {
      this.carResourceList.cover.push(response.data);
      this.carForm.cover = response.data;
    },
    //文件删除
    // file 当前要删除的文件  里面包含的response是后端上传接口返回的值
    // fileList 当前未被删除的所有的文件
    coverHandleRemove(file, fileList) {
      var filePath = file.response;    // 当前要删除文件的url地址
      axios.delete("/fastdfs?path=" + filePath)
          .then(res => {
            if (res.data.success) {
              this.$message({
                message: '删除成功!',
                type: 'success'
              });
              // 将shop.logo设置为null,避免将已经删除的图片的路径带到数据库
              this.carForm.cover = "";
            } else {
              this.$message({
                message: '删除失败!',
                type: 'error'
              });
            }
          })
    },

    search() {
      this.query.currentPage = 1;
      this.getCars();
    },
    handleCurrentChange(val) {  // val是当前页
      this.query.currentPage = val;
      this.getCars();
    },
    //获取部门列表
    getCars() {
      this.listLoading = true;  // 弹出加载框
      axios.put("/car", this.query)   // axios.defaults.baseURL+/car  http://localhost:80/api/car
          .then(result => {
            let {code, success, data, message} = result.data; // result.data  {success:true,message:"",result:{total:1,rows:[]}}
            if (success) {
              this.pageInfo.total = data.total;
              this.pageInfo.rows = data.data;
              this.listLoading = false;  // 关闭加载框
            } else {
              this.$message({message: '查询分页失败!' + message, type: 'error'});
            }
          })
          .catch(result => {
            this.$message({message: "查询分页失败!", type: 'error'});
          })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        axios.delete("/car/" + row.id)
            .then(result => {
              result = result.data;
              if (result.success) {
                this.$message({message: '删除成功', type: 'success'});
                // 刷新列表
                this.query.currentPage = 1;
                this.getCars();
                this.listLoading = false;  // 关闭弹出框
              } else {
                this.$message({message: '删除失败!' + result.message, type: 'error'});
              }
            })
      }).catch(() => {
        this.$message({message: '已取消删除', type: 'info'});
      });
    },
    getEmployeeList() {
      axios.get("/employee")
          .then(result => {
            result = result.data;
            if (result.success) {
              this.employees = result.result;
            } else {
              this.$message({message: '获取员工列表失败!' + result.message, type: 'error'});
            }
          }).catch(result => {
        this.$message({message: '获取员工列表失败!', type: 'error'});
      })
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.carFormVisible = true;
      this.carForm = Object.assign({}, row);
      if (this.carForm.manager == null) {
        this.carForm.manager = {id: null}
      }
      if (row.path != null) {
        let arrays = row.path.split("/");    // ['', '1', '2', '20'] 根据/分割之后,第一个位置是一个空字符串,是不需要的,另外最后一个值是当前部门的id,而不是父部门的id,所以也不需要
        let ids = [];
        for (let i = 1; i < arrays.length - 1; i++) {
          ids.push(parseInt(arrays[i]))    // 数组分割之后里面的值都是字符串,而部门树的id都是数字,他们匹配不上就不能回显,因此需要parseInt()转一下
        }
        this.carForm.parent = ids;
      }
      //this.carForm.parent = [1,2];
      // 获取所有的员工数据
      this.getEmployeeList();
      // 获取部门树
      this.getDeptTree();
    },
    //显示新增界面
    handleAdd: function () {
      // 弹出弹框
      this.carFormVisible = true;
      // 清空双向绑定的数据,避免将上一次填写的数据带到下一次
      this.carForm = {
        id: null,
        title: "",
        cover: "",
        saleprice: 0,
        costprice: 0,
        isnew: 0,
        reigstertime: "",
        mileage: null,
        costeffective: 0,
        rushsale: 0,
        quasinewcar: 0,
        transitivecountry: 0,
        type: {
          id: null,
          name: ""
        },
        typeId: null,
        carDetail: {
          info: ""
        }
      };
      // 调用查询车辆类型数,渲染下拉框
      this.getCarTypeTree();
      this.getCarTypes();
    },
    getCarTypes() {
      axios.get("/car/carTypes").then(res => {
        if (res.status == 200) {
          this.carTypes = res.data.data;
        }
      })
    },
    getCarTypeTree() {
      axios.get('/carType/treeData')
          .then(res => {

            if (res.data.success) {
              this.carTypeTree = res.data.data;
            }
          })
    },
    //新增修改保存方法
    save: function () {
      this.$refs.carForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.carFormLoading = true;
            let para = Object.assign({}, this.carForm);
            // 处理车辆类型,如果选择了车辆类型的话,它双向绑定的type是一个数组,将数组的最后一个位置的值赋值给typeId
            if (para.type && para.type.length > 0) {
              para.typeId = para.type[para.type.length - 1];
            }
            axios.post("/car", para)
                .then(result => {
                  result = result.data;
                  if (result.success) {
                    this.$message({message: '保存成功', type: 'success'});
                    this.carFormLoading = false;
                    this.query.currentPage = 1;
                    this.getCars();
                    this.carFormVisible = false;
                  } else {
                    this.$message({message: '保存失败!' + result.message, type: 'error'});
                  }
                }).catch(result => {
              this.$message({message: '保存失败!', type: 'error'});
            })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = {ids: ids};
        batchRemoveUser(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getCars();
        });
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getCars();  // 做分页查询
  }
}

</script>

<style scoped>

</style>